package com.ocean.array;

import java.util.Arrays;

/**
 * https://leetcode.cn/problems/plus-one/
 */
public class PlusOne {

    public static void main(String[] args) {
        int[] digits = {9, 9, 9};
//        int[] digits = {1, 2, 3};
        System.out.println(Arrays.toString(digits));
        System.out.println(Arrays.toString(new PlusOne().plusOne(digits)));
    }

    public int[] plusOne(int[] digits) {
        if (digits == null || digits.length == 0) {
            return digits;
        }
        // 找出第一个不是9的
        int n = digits.length;
        for (int i = n - 1; i >= 0; i--) {
            if (digits[i] != 9) {
                ++digits[i];
                for (int j = i + 1; j < n; j++) {
                    digits[j] = 0;
                }
                return digits;
            }
        }
        int[] newDigits = new int[n + 1];
        newDigits[0] = 1;
        return newDigits;
    }
}
